package br.com.cardapiolivre.persistencia;

import br.com.cardapiolivre.entidades.Conta;
import br.com.cardapiolivre.entidades.Entidade;
import java.util.List;
import javax.persistence.Query;

/**
 *
 * @author juan
 */
public class ContaDaoJpa extends DaoJpa<Conta> {

    @Override
    public Conta consultar(Long id) throws Exception {
        try {
            return (Conta) em.find(Conta.class, id);
        } catch (Exception ex) {
            throw ex;
        }
    }

    @Override
    public List<Conta> consultar() throws Exception {
        try {
            Query q = em.createQuery("SELECT c FROM Conta c ");
            return q.getResultList();
        } catch (Exception ex) {
            ex.printStackTrace();
            throw new Exception("Erro ao consultar conta.");
        }
    }

    public boolean verificarEmail(String email) throws Exception {
        try {
            Query q = em.createQuery("SELECT c FROM Conta c WHERE c.email = '"+email+"'");            
            return q.getResultList().size() > 0;
        } catch (Exception ex) {
            ex.printStackTrace();
            throw new Exception("Erro ao verificar a validade do email.");
        }
    }
}
